/*************************************************************************
    > File Name: Accountbook_main.cpp
    > Author: skylon
    > Site: blog.csdn.net/tcm_zhangpeng
    > Created Time: 2016年05月15日 星期日 20时01分26秒
 ************************************************************************/

#include"Accountbook.h"
/*
 *	simulate user take/save money process
 */
int main()
{
	ifstream InputStream("Account.in");	//	open the file you want read
	
	string AccountNo;
	string AccountPassword;
	double AccountCount;
	string ItemName;
	double OldAccountbook;
	
	Accountbook MyAccountbook;
	
	AccountItem FoundItem;
	
	string TransactionCode;

	MyAccountbook.LoadAccountbook(InputStream);

	cout << "Please input account number: " << endl;
	cin >> AccountNo;
	FoundItem = MyAccountbook.FindItem(AccountNo);
	if (FoundItem.IsNull())
	{
		cout << "Account isn't exist!" << endl;
		return 0;
	}

	cout << "Please input account password: " << endl;
	cin >> AccountPassword;
	if (FoundItem.GetPassword() != AccountPassword)
	{
		cout << "Wrong password!" << endl;
		return 0;
	}

	OldAccountbook = FoundItem.GetBalance();
	cout << "Pleaae chose transaction code: " << endl << endl;
	cout << "G	---	take money" << endl << endl;
	cout << "C	---	query money" << endl << endl;
	cin >> TransactionCode;

	if (TransactionCode == "C" || TransactionCode == "c")
	{
		cout << "Your balance is : " << endl;
		cout << FoundItem.GetBalance() << endl;
	}
	else if (TransactionCode == "G" || TransactionCode == "g")
	{
		cout << "Please input amount of money you take: " << endl;
		cin >> AccountCount;
		if (OldAccountbook < AccountCount)
		{	
			cout << "Your balance is not enough!" << endl;
		}
		else
		{	
			FoundItem.DeductBalance(AccountCount);
			MyAccountbook.UpdateItem(FoundItem);
			cout << "Your balance is: " << endl;
			cout << FoundItem.GetBalance() << endl;
			ofstream OutputStream("Account.in");	//	writte information in file
			MyAccountbook.StoreAccountbook(OutputStream);
		}
	}
	else
	{
		cout << "Wrong transaction code!" << endl;
	}
	return 0;
}











